package com.easyidea.its.util.excel;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.easyidea.its.util.DateUtil;
import com.easyidea.its.web.dto.StatisticsDto;
import com.easyidea.its.web.dto.StatisticsExport;

public class ExportDataUtil {
	public static List<StatisticsExport> createDataset(StatisticsDto statisticsDto,
			Collection<Object[]> objs) {

		// row keys...
		final List<StatisticsExport> dataset = new ArrayList<StatisticsExport>();
		final String series1 = "";
		if (statisticsDto.getStatisticsType().equals("hour")) {
			createDataSetByHour(statisticsDto, objs, dataset);
		} else if (statisticsDto.getStatisticsType().equals("day")) {
			createDataSetByDay(statisticsDto, objs, dataset);
		} else if (statisticsDto.getStatisticsType().equals("month")) {
			createDataSetByMonth(statisticsDto, objs, dataset);
		}

		// create the dataset...

		return dataset;

	}

	private static void  createDataSetByMonth(StatisticsDto statisticsDto,
			Collection<Object[]> objs, final List<StatisticsExport> dataset) {
		Map<Integer, Integer> dbMap = new HashMap<Integer, Integer>();
		Iterator it = objs.iterator();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			Integer key = null;
			if (o[0] != null) {
				key = (Integer) o[1];
			}
			BigInteger num = (BigInteger) o[0];
			dbMap.put(key.intValue(), num.intValue());
		}
		long day = statisticsDto.getEndMonth() - statisticsDto.getStartMonth();
		for (int i = 0; i <= day; i++) {

			Integer val = statisticsDto.getStartMonth() + i;
			if (dbMap.get(val) != null) {
				dataset.add(new StatisticsExport(String.valueOf(dbMap.get(val)), String
						.valueOf(statisticsDto.getStartYear())
						+ "/" + String.valueOf(val)));
			} else {
				dataset.add(new StatisticsExport(String.valueOf(0),  String.valueOf(statisticsDto
						.getStartYear())
						+ "/" + String.valueOf(val)));
			}

		}
	}

	private static void createDataSetByDay(StatisticsDto statisticsDto,
			Collection<Object[]> objs, final List<StatisticsExport> dataset) {
		Date dStart = statisticsDto.getDayStartDate();
		Date dEnd = statisticsDto.getDayEndDate();
		long day = DateUtil.getdate(dEnd, dStart);
		Map<Date, Integer> dbMap = new HashMap<Date, Integer>();
		Iterator it = objs.iterator();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			Date key = null;
			if (o[0] != null) {
				key = ((Date) o[1]);
			}
			BigInteger num = (BigInteger) o[0];
			dbMap.put(key, num.intValue());
		}
		for (int i = 0; i <= day; i++) {
			String today = String.valueOf(DateUtil.getYear(dStart)) + "/"
					+ String.valueOf(DateUtil.getMonth(dStart)) + "/"
					+ (String.valueOf(DateUtil.getDay(dStart) + i ));
			if (dbMap.get(DateUtil.addDate(dStart, i)) != null) {

				dataset.add(new StatisticsExport(String.valueOf(dbMap.get(DateUtil.addDate(dStart, i))),
						String.valueOf(today)));
			} else {
				dataset.add(new StatisticsExport(String.valueOf(0), String.valueOf(today)));
			}
		}
	}

	private static void createDataSetByHour(StatisticsDto statisticsDto,
			Collection<Object[]> objs, final List<StatisticsExport> dataset) {
		Map<Integer, Integer> dbMap = new HashMap<Integer, Integer>();
		Iterator it = objs.iterator();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			Integer key = null;
			if (o[0] != null) {
				key = (Integer) o[1];
			}
			BigInteger num = (BigInteger) o[0];
			dbMap.put(key.intValue(), num.intValue());
		}
		for (int i = 0; i <= statisticsDto.getEndHour()
				- statisticsDto.getStartHour(); i++) {
			Integer val = statisticsDto.getStartHour() + i;
			if (dbMap.get(val) != null) {
				dataset.add(new StatisticsExport(String.valueOf(dbMap.get(val)), String
						.valueOf(val)
						+ "时"));
			} else {
				dataset.add(new StatisticsExport(String.valueOf(0),  String.valueOf(val) + "时"));
			}

		}
	}
}
